<template>
	<view class="artwork" id="arc-content">
		<uni-nav-bar :fixed="true" left-icon="back" :border="false" backgroundColor="#f9f2ea" leftText="作品详情"
			:statusBar="true" @clickLeft="goBack"></uni-nav-bar>
		<view class="information">
			<view class="information-top">
				<image class="img" :src="artworkData.thumb" mode="aspectFit"></image>
			</view>
			<view class="information-content">
				<view class="left">
					<view class="price">
						￥{{artworkData.goods_price}}
					</view>
					<view class="title">
						{{artworkData.name}}
					</view>
				</view>
				<view class="right">
					<view class="flex-between" @click="collect(artworkData.is_collect)">
						<image class="img" v-show="artworkData.is_collect == false" src="../../static/collect.png"
							mode=""></image>
						<image class="img" v-show="artworkData.is_collect == true" src="../../static/mine_collect.png"
							mode=""></image>
					</view>
					<button open-type="share" class="location">
						<image class="img" src="../../static/share.png" mode=""></image>
					</button>
					<view class="collect" @click="doSame">
						做同款
					</view>
				</view>
			</view>
			<view class="list" v-for="(items, index) in goods" :key="index" v-if="items.list.length>0">
				<view class="tips">
					<view class="tips-left">
						{{typeRequest(items.goods_type)}}
					</view>
				</view>
				<view class="items-list">
					<view class="item" v-for="(item, idx) in items.list" :key="idx">
						<image class="img" :src="item.goods_thumb" mode="aspectFit"></image>
						<view class="title flex-between">
							{{item.goods_name}}
						</view>
						<view class="price">
							￥{{item.goods_amount}}
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="btn-group">
			<view class="btn cart" @click="clickShow(1)">
				加入购物车
			</view>
			<view class="btn buy" @click="clickShow(2)">
				立即购买
			</view>
		</view>

		<view class="mark" v-show="subscribeShow">
			<view class="mark-bg" @click="subscribeShow = false">

			</view>
			<view class="mark-box">
				<view class="box-top flex">
					<view class="top-left">
						<image class="img" :src="artworkData.thumb" mode=""></image>
					</view>
					<view class="top-center">
						<view class="price">
							￥{{artworkData.goods_price}}
						</view>
						<view class="">
							{{artworkData.spec_name}}
						</view>
					</view>
					<view class="top-right">
						<image class="img" @click="subscribeShow = false" src="../../static/search_delete.png" mode="">
						</image>
						<view class="number-box flex">
							<view class="reduce frame" @click="reduce">
								<image class="img" v-if="buyNumber>1" src="../../static/reduce.png" mode=""></image>
								<image class="img" v-else src="../../static/unreduce.png" mode=""></image>
							</view>
							<view class="number">
								{{buyNumber}}
							</view>
							<view class="add frame" @click="add">
								<image class="img" v-if="buyNumber<99999" src="../../static/add.png" mode=""></image>
								<image class="img" v-else src="../../static/unadd.png" mode=""></image>
							</view>
						</view>
					</view>
				</view>
				<view class="btn-group flex">
					<view class="btn confirm" @click="confirmBuy()">
						确认
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		wxLogin,
		userInfo,
		artDetail,
		getGoodsComment,
		addCollect,
		cancelCollect,
		addCart,
		accountOrder
	} from "@/api/public.js"
	import jyfParser from '@/components/jyf-parser/jyf-parser.vue'
	export default {
		data() {
			return {
				commentList: [],
				goods: [{
						goods_type: 1,
						list: [],
					},
					{
						goods_type: 2,
						list: [],
					},
					{
						goods_type: 3,
						list: [],
					},
					{
						goods_type: 4,
						list: [],
					},
				],
				specList: [],
				id: "",
				artworkData: {},
				commentPage: 1,
				commentTotal: 0,
				buyNumber: 1,
				subscribeShow: false,
				buyType: 0,
			}
		},
		methods: {
			goBack() {
				uni.navigateBack();
			},
			goBack() {
				uni.navigateBack();
			},
			// 获取商品信息
			getInfo() {
				artDetail(this.id).then(res => {
					this.artworkData = res.reData.data;
					this.artworkModel = JSON.parse(this.artworkData.data);
					console.log(this.artworkData)
					let arr = res.reData.data.detail;
					for (let i in arr) {
						switch (arr[i].goods_type) {
							case 1:
								this.goods[0].list.push(arr[i]);
								break;
							case 2:
								this.goods[1].list.push(arr[i]);
								break;
							case 3:
								this.goods[2].list.push(arr[i]);
								break;
							case 4:
								this.goods[3].list.push(arr[i]);
								break;
						}
					}
				})
			},
			// 获取评价
			getComment() {
				getGoodsComment(this.id, this.commentPage).then(res => {
					this.commentTotal = res.reData.total;
					this.commentList = res.reData.data;
				})
			},
			// 锚点跳转到评价
			toCommentAddress() {
				uni.createSelectorQuery().select('#comment-content').boundingClientRect(data => { //目标位置的节点：类或者id
					uni.createSelectorQuery().select("#arc-content").boundingClientRect(res => { //最外层盒子的节点：类或者id
						uni.pageScrollTo({
							duration: 100, //过渡时间
							scrollTop: data.top - res.top, //到达距离顶部的top值
						})
					}).exec()
				}).exec();
			},
			// 收藏取消收藏
			collect(type) {
				if (type) {
					cancelCollect(this.id, 3).then(res => {
						if (res.result == 0) {
							this.artworkData.is_collect = false;
							this.$forceUpdate();
						}
					})
				} else {
					addCollect(this.id, 3).then(res => {
						if (res.result == 0) {
							this.artworkData.is_collect = true;
							this.$forceUpdate();
						}
					})
				}
			},
			confirmBuy() {
				if (this.buyType == 1) {
					this.addShoppingCart()
				} else if (this.buyType == 2) {
					this.bayNow();
				}
			},
			addShoppingCart() {
				addCart(this.artworkData.id, this.buyNumber, 2).then(res => {
					console.log(res)
					if (res.result == 0) {
						uni.showToast({
							title: "加入成功",
							icon: "success",
						})
					}
				})
			},

			doSame() {
				if (uni.getStorageSync("userData")) {

					uni.navigateTo({
						url: "../mine/doSame?id=" + this.id + "&baseId=" + this.artworkModel.baseId
					})
				} else {
					uni.navigateTo({
						url: "/pages/index/choose"
					})
				}
			},
			bayNow() {
				let arr = [{
					"goods_id": this.artworkData.id,
					"goods_num": this.buyNumber,
					"goods_type": 2
				}];
				let str = JSON.stringify(arr);
				accountOrder(str).then(res => {
					if (res.result == 0) {
						uni.setStorageSync("accountsType", 1);
						uni.navigateTo({
							url: "../shoppingCart/confirmOrder?goods=" + str + "&orderType=2"
						})
					}
				})
			},
			reduce() {
				if (this.buyNumber > 1) {
					this.buyNumber--;
				}
			},
			add() {
				if (this.buyNumber >= 99999) {
					uni.showToast({
						title: "库存不足",
						icon: "none"
					})
				} else if (this.buyNumber < 99999) {
					this.buyNumber++;
				}

			},
			//点击购买或加入购物车
			clickShow(buyType) {
				console.log(this.artworkData.status)
				if (this.artworkData.status == 0) {
					this.subscribeShow = true;
					this.buyType = buyType;
				} else {
					uni.showToast({
						title: "部分配件已经下架无法再次购买！",
						icon: "none"
					})
				}
			}

		},
		onLoad(obj) {
			if (uni.getStorageSync("sessionId")) {

			} else {
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						wxLogin(loginRes.code).then(res => {
							uni.setStorageSync("sessionId", res.reData.sessionId);
							userInfo().then(res => {
								if (res.reData.data) {
									uni.setStorageSync("userData", JSON.stringify(res.reData
										.data));
								}
							})
						});
					},
				});
			}

			this.id = obj.id;
			this.getInfo();
			wx.showShareMenu({
				withShareTicket: false,
				menus: ['shareAppMessage', 'shareTimeline']
			})
		},
		computed: {
			typeRequest() {
				return function(type) {
					switch (type) {
						case 1:
							return "主体"
						case 2:
							return "配件"
						case 3:
							return "釉"
						case 4:
							return "贴图"
					}
				}
			}
		},
		components: {
			jyfParser
		},
		// 分享配置
		onShareAppMessage(res) {
			return {
				title: this.artworkData.name,  
				path:"pages/hypermarket/artDetails?id="+this.artworkData.id,
				imageUrl: this.artworkData.thumb
			}
		},
		// 分享配置
		onShareTimeline(res) {
			return {
				title: this.artworkData.name,  
				path:"pages/hypermarket/artDetails?id="+this.artworkData.id,
				imageUrl: this.artworkData.thumb
			}
		},
		
	}
</script>



<style lang="scss" scoped>
	.artwork {
		padding-bottom: 60rpx;
	}

	.information {
		padding: 30rpx;
		margin-bottom: 30rpx;

		.information-top {
			position: relative;
			width: 690rpx;
			height: 690rpx;
			margin-bottom: 40rpx;

			.img {
				width: 100%;
				height: 100%;
			}

			background-image: url(../../static/art_bg.png);
			background-size: 100% 100%;

			// .img {
			// 	width: 120%;
			// 	position: absolute;
			// 	top: 170rpx;
			// 	left: 50%;
			// 	transform: translate(-50%);
			// 	// margin-top:200rpx;
			// 	// height: 200rpx;
			// }
		}

		.information-content {
			display: flex;
			justify-content: space-between;
			align-items: center;
			background: #fffaf5;
			border-radius: 20rpx;
			overflow: hidden;
			padding: 20rpx 30rpx;
			margin-bottom: 20rpx;
			box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.11);

			.left {
				.price {
					font-size: 32rpx;
					line-height: 36rpx;
					color: rgba(216, 127, 129, 1);
				}

				.title {
					font-size: 28rpx;
					line-height: 32rpx;
					color: rgba(140, 111, 111, 1);
					max-width: 360rpx;
				}
			}

			.right {
				display: flex;
				align-items: center;

				.img {
					width: 48rpx;
					height: 48rpx;
				}

				.location {
					padding: 0;
					margin: 0;
					border: none;
					font-size: 22rpx;
					line-height: 34rpx;
					color: #999;
					width: 48rpx;
					height: 48rpx;
					margin: 0 30rpx;

					&::after {
						border: none;
					}
				}

				.collect {
					width: 120rpx;
					height: 72rpx;
					background-color: rgba(214, 142, 127, 1);
					border-radius: 20rpx;
					line-height: 72rpx;
					font-size: 32rpx;
					color: #fff;
					text-align: center;
				}
			}
		}

		.tips {
			box-sizing: border-box;
			padding: 30rpx 0;
			font-size: 30rpx;
			line-height: 30rpx;

			.tips-left {
				height: 30rpx;
				color: rgba(140, 111, 111, 1);
				font-weight: 700;
			}
		}
	}

	.list {
		.items-list {
			display: flex;
			flex-wrap: wrap;

			.item {
				margin-right: 20rpx;
				margin-bottom: 20rpx;
				border-radius: 10rpx;
				background: #fffaf5;
				box-shadow: 0rpx 2rpx 4rpx rgba(189, 163, 112, 0.25);

				.img {
					width: 200rpx;
					height: 200rpx;
					border-radius: 10rpx;
				}

				.title {
					padding: 0 10rpx;
					font-size: 24rpx;
					color: rgba(140, 111, 111, 1);
					line-height: 36rpx;
				}

				.price {
					padding: 0 10rpx;
					font-size: 24rpx;
					color: rgba(69, 31, 31, 1);
					line-height: 36rpx;
				}
			}
		}
	}

	.detail {
		padding: 30rpx;
		background: #fff;
		margin-bottom: 30rpx;

		.detail-top {
			text-align: center;
			font-size: 28rpx;
			line-height: 52rpx;
			color: #333;
		}

		.title {
			box-sizing: border-box;
			padding: 30rpx 0;
			font-size: 24rpx;
			line-height: 24rpx;
			background: #fff;

			.title-left {
				height: 24rpx;
				color: #666;
				border-left: 6rpx solid #cd2e40;
				padding-left: 24rpx;
			}
		}

		.size {
			font-size: 28rpx;
			line-height: 28rpx;
			margin-bottom: 30rpx;
		}

		/deep/ image {
			width: 100%;
		}
	}

	.btn-group {
		position: fixed;
		width: 100%;
		bottom: 0;
		height: 120rpx;
		overflow: hidden;
		display: flex;
		align-items: center;
		justify-content: space-around;
		background-color: rgba(255, 250, 245, 1);

		z-index: 10;

		.btn {
			box-shadow: 10rpx 10rpx 20rpx rgba(189, 185, 166, 0.5);
			height: 88rpx;
			font-size: 30rpx;
			line-height: 88rpx;
			color: #fff;
			text-align: center;

			&.cart {
				width: 220rpx;
				background-color: rgba(255, 250, 245, 1);
				border: 2rpx solid rgba(214, 142, 127, 1);

				border-radius: 50rpx;
				color: rgba(214, 142, 127, 1);
			}

			&.buy {
				width: 400rpx;
				background: #ce2e3a;
				background-color: rgba(214, 142, 127, 1);


				border-radius: 50rpx;
			}

			&.confirm {
				width: 220rpx;
				background-color: rgba(214, 142, 127, 1);

				border-radius: 50rpx;
			}
		}
	}

	.mark {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		z-index: 10;

		.mark-bg {
			width: 100%;
			height: 100%;
			background: rgba(0, 0, 0, 0.5);
		}

		.mark-box {
			position: absolute;
			background: #fffaf5;
			padding-bottom: 120rpx;
			left: 0;
			right: 0;
			bottom: 0;
			margin: 0 auto;
			border-radius: 20rpx 20rpx 0 0;

			.box-top {
				padding: 30rpx;

				.top-left {
					margin-right: 20rpx;

					.img {
						width: 160rpx;
						height: 160rpx;
						border-radius: 10rpx;
					}
				}

				.top-center {
					flex-grow: 1;
					font-size: 28rpx;
					color: rgba(140, 111, 111, 1);
					line-height: 36rpx;
					word-break: break-all;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-line-clamp: 5;
					-webkit-box-orient: vertical;
					overflow: hidden;

					.price {
						color: rgba(214, 142, 127, 1);
					}
				}

				.top-right {
					width: 240rpx;
					display: flex;
					margin-left: 20rpx;
					flex-wrap: wrap;
					justify-content: flex-end;

					.img {
						width: 40rpx;
						height: 40rpx;
					}

					.number-box {
						padding: 20rpx;

						.frame {
							width: 48rpx;
							height: 48rpx;

							.img {
								width: 100%;
								height: 100%;
							}
						}

						.number {
							margin: 0 30rpx;
							line-height: 46rpx;
							color: #333;
							font-size: 26rpx;
						}
					}
				}
			}

		}
	}
</style>
